package cn.edu.cqupt.cxcy.timerTask;

import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * 定时器所依赖的监听器
 */
@WebListener
public class ContextListener implements ServletContextListener {

	private java.util.Timer timer = null;  
	
    public ContextListener() {}

    public void contextInitialized(ServletContextEvent event)  { 
    	
    	/** 
        * 设置一个定时器 
        */  
       timer = new java.util.Timer(true);  
//       event.getServletContext().log("定时器已启动");  
       /** 
        * 定时器到指定的时间时,执行某个操作(如某个类,或方法) 
        */  
       //后边最后一个参数代表	,现在为一小时  
       timer.schedule(new MyTask(event.getServletContext()), 0, 60 * 60 * 1000);  
//       event.getServletContext().log("已经添加任务调度表");  
    }

    public void contextDestroyed(ServletContextEvent event)  { 
    	timer.cancel();  
//        event.getServletContext().log("定时器销毁");  
    }
	
}
